package cn.maszy.common;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.maszy.model.projectmanagement.C_T_Sys_File;
import cn.maszy.model.projectmanagement.C_T_Sys_FileType;
import cn.maszy.service.projectmanagement.C_T_Sys_FileService;
import cn.maszy.service.projectmanagement.C_T_Sys_FileTypeService;

public class UploadFileAction extends BaseAction<C_T_Sys_File>{
	private static final long serialVersionUID = 1L;
	@Autowired
	private C_T_Sys_FileService c_T_Sys_FileService;
	@Autowired
	private C_T_Sys_FileTypeService c_T_Sys_FileTypeService;
	private C_T_Sys_File c_T_Sys_File;
	private List<C_T_Sys_FileType> list_C_T_Sys_FileType;
	private File fileInput;
	private String fileInputFileName;// Struts2的拦截器FileUploadInterceptor根据请求对象中参数"fileInput"来自行生产的一个固有属性
	private String uuid;
	private String moduleName;
	//全选
	private String checkIds;
	private String typeid;
	private String typename;
	private String filetypeId;
	private HttpServletRequest request;  
    private HttpServletResponse response;  
    private String savePath;
    private String fileName;
    private String projectUuid;
    private String flag;
    private String auxiliaryId;
	/*********************class set/get****************************/
	public File getFileInput() {
		return fileInput;
	}

	public void setFileInput(File fileInput) {
		this.fileInput = fileInput;
	}

	public String getFileInputFileName() {
		return fileInputFileName;
	}

	public void setFileInputFileName(String fileInputFileName) {
		this.fileInputFileName = fileInputFileName;
	}

	public C_T_Sys_File getC_T_Sys_File() {
		return c_T_Sys_File;
	}

	public void setC_T_Sys_File(C_T_Sys_File c_T_Sys_File) {
		this.c_T_Sys_File = c_T_Sys_File;
	}

	public String getCheckIds() {
		return checkIds;
	}

	public void setCheckIds(String checkIds) {
		this.checkIds = checkIds;
	}
	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public String getTypename() {
		return typename;
	}

	public void setTypename(String typename) {
		this.typename = typename;
	}
	public List<C_T_Sys_FileType> getList_C_T_Sys_FileType() {
		return list_C_T_Sys_FileType;
	}

	public void setList_C_T_Sys_FileType(
			List<C_T_Sys_FileType> list_C_T_Sys_FileType) {
		this.list_C_T_Sys_FileType = list_C_T_Sys_FileType;
	}
	public String getFiletypeId() {
		return filetypeId;
	}

	public void setFiletypeId(String filetypeId) {
		this.filetypeId = filetypeId;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getProjectUuid() {
		return projectUuid;
	}

	public void setProjectUuid(String projectUuid) {
		this.projectUuid = projectUuid;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String getAuxiliaryId() {
		return auxiliaryId;
	}

	public void setAuxiliaryId(String auxiliaryId) {
		this.auxiliaryId = auxiliaryId;
	}

	/*******************STRUTS ACTION METHOD*************************/
	/**
	 * 文件上传
	 */
	@SuppressWarnings("deprecation")
	public String fileUpdate() throws Exception
	{
		//获取实体路径
		String savePath = ServletActionContext.getRequest().getRealPath("");
		savePath = savePath + "\\uploadFile\\";
		String extName = "";// 扩展名
		String newFileName = "";// 新文件名
		
		// 获取扩展名
		if (fileInputFileName.lastIndexOf(".") > -1) {
			extName = fileInputFileName.substring(fileInputFileName.lastIndexOf("."));
		}
		String nowTime = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());// 当前时间
		newFileName = nowTime + extName;
		
		String sts = ServletActionContext.getRequest().getLocalAddr()+":"+ServletActionContext.getRequest().getLocalPort();
		//获取URL
		String url = "http://"+sts+"/zy-manage/uploadFile/"+newFileName;
		fileInput.renameTo(new File(savePath + newFileName));
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		C_T_Sys_File c_t_sys_file = new C_T_Sys_File();
		String fileID = UUID.randomUUID().toString();
		//日期
		String str = nowTime.substring(0,14).toString();
		SimpleDateFormat format = new SimpleDateFormat();
		format.applyPattern("yyyyMMddHHmmSS");
		Date date = format.parse(str);
		format.applyPattern("yyyy-MM-dd HH:mm:SS");
		response.getWriter().print(format.format(date)+"-->"+SessionFactory.getSessionUserRealNam()+"-->"+fileInputFileName+","+fileID+","+url);
		//赋值写入数据库
		
		c_t_sys_file.setFileId(fileID);
		c_t_sys_file.setModuleName(moduleName);
		c_t_sys_file.setUuid(uuid);
		c_t_sys_file.setAuxiliaryId(auxiliaryId);
		c_t_sys_file.setTypeId(typeid);
		c_t_sys_file.setType(typename);
		//改变后文件名
		c_t_sys_file.setFileName(fileInputFileName);
		//文件保存路径
		c_t_sys_file.setFilePath(savePath + newFileName);
		c_t_sys_file.setFileUflPath(url);
		c_t_sys_file.setFileTime(DateUtils.nows());
		
		c_T_Sys_FileService.update(c_t_sys_file);
		return null;
	}
	/**
	 * 查询
	 * @return
	 */
	public  String fileList() 
	{
		pager = new Pager<C_T_Sys_File>(pageNum, numPerPage,orderField,orderDirection);
		pager= c_T_Sys_FileService.selectList(c_T_Sys_File,pager,filetypeId,moduleName,projectUuid);
		return SUCCESS;
	}
	/**
	 * 查询附件类型
	 */
	public String fileTypeList()
	{
		C_T_Sys_FileType c_T_Sys_FileType = new C_T_Sys_FileType();
		list_C_T_Sys_FileType = c_T_Sys_FileTypeService.selectLists(c_T_Sys_FileType,moduleName);
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String fileDel()
	{
		int i=c_T_Sys_FileService.delete(checkIds);
		if(i>0)
		return ajaxForwardSuccess(getText("msg.operation.success"), "fileTypeList","jbsxBox", "forward", "projectmanagement/fileTypeList?moduleName=project");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	/**
	 * 跟句文件名称删除附件
	 * @return
	 */
	public String fileDelete(){
		int i=c_T_Sys_FileService.delete(checkIds);
		if(i > 0){
			return ajaxForwardSuccess(getText("msg.operation.success"), "projectmanagement/TaskManageDital?b_T_Ufp_TaskManager.taskid="+projectUuid,"relx", "forward", "projectmanagement/TaskManageDital?b_T_Ufp_TaskManager.taskid="+projectUuid);
		}else{
			return ajaxForwardSuccess(getText("msg.operation.success"), "projectmanagement/ProjectmanagementJump?b_T_Flow_ProjectManagement.projectUuid="+projectUuid,"relx", "forward", "projectmanagement/ProjectmanagementJump?b_T_Flow_ProjectManagement.projectUuid="+projectUuid);
		}
	}
	//跳转到新建页面
	public String fileAdd()
	{
		if(c_T_Sys_File==null){
			c_T_Sys_File=c_T_Sys_FileService.selectOne("");
		}else{
			c_T_Sys_File=c_T_Sys_FileService.selectOne(c_T_Sys_File.getFileId());
		}
		return SUCCESS;
	}
}
